package com.bjpowernode;

import com.microsoft.playwright.*;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Random;
import java.util.UUID;

public class AppTest1 {

    @Test
    public void gitee() {
        HashMap<String,String> env = new HashMap<>();
        env.put("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD","1");
        env.put("PLAYWRIGHT_BROWSERS_PATH","C:\\Users\\admin\\AppData\\Local\\ms-playwright");
        env.put("PWDBUG","console");
        try (
                Playwright playwright = Playwright.create(new Playwright.CreateOptions().setEnv(env))) {
//            PLAYWRIGTH_JAVA_SRC=SRC/TEST/JAVA
            BrowserType chromium = playwright.chromium();
            Browser browser = chromium.launch(new BrowserType.LaunchOptions().setHeadless(false).setChannel("chrome"));
            BrowserContext context = browser.newContext();
            Page page = context.newPage();
            page.navigate("https://www.baidu.com");
            page.locator("#kw").click();
            page.locator("#kw").fill("gitee");
            page.locator("#kw").press("Enter");
            page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get(UUID.randomUUID().toString()+".png")));
            System.out.println(page.title());
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Test
    public void qqmail() {
        HashMap<String,String> env = new HashMap<>();
        env.put("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD","1");
        env.put("PLAYWRIGHT_BROWSERS_PATH","C:\\Users\\admin\\AppData\\Local\\ms-playwright");
        env.put("PWDBUG","console");
        try (
                Playwright playwright = Playwright.create(new Playwright.CreateOptions().setEnv(env))) {
//            PLAYWRIGTH_JAVA_SRC=SRC/TEST/JAVA
            BrowserType chromium = playwright.chromium();
            Browser browser = chromium.launch(new BrowserType.LaunchOptions().setHeadless(false).setChannel("chrome"));
            BrowserContext context = browser.newContext();
            Page page = context.newPage();
            page.navigate("https://www.baidu.com");
            page.locator("#kw").click();
            page.locator("#kw").fill("qq.mail");
            page.locator("#kw").press("Enter");
            page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get(UUID.randomUUID().toString()+".png")));
            System.out.println(page.title());
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
